<template>
	<view>
		<wd-form ref="form" :model="info">
			<wd-cell-group border>
		
				<wd-textarea label="今日总结" label-width="100px" prop="descript" v-model="info.descript" placeholder="请填写今日总结"
					:rules="[{ required: true, message: '请填写今日总结' }]" />
				<wd-textarea label="明日计划" label-width="100px" prop="next_day" v-model="info.next_day" placeholder="请填写明日计划"
					:rules="[{ required: true, message: '请填写明日计划' }]" />
			</wd-cell-group>
			<view class="mtp mxp">
				<wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
			</view>
		</wd-form>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue';
	import { putdaily , postdaily , getdailyDetail } from "@/api/form.js"
	import {
		onLoad,
		onShow,
		onPullDownRefresh
	} from '@dcloudio/uni-app'

	const info = reactive({
		"descript": "",
		"next_day" : ""
	})
	
	const form = ref()
	const type = ref("create")
	const id = ref()
	
	const getDetail = () =>{
		 getdailyDetail(id.value).then(res =>{
			info.descript = res.descript
			info.next_day = res.next_day
		})
	}
	
	onLoad((e)=>{
		if(e.id){
			id.value = e.id
			type.value = "edit"
			getDetail()
		}else{
			type.value = "create"
		}
	})

	const handleSubmit = (e) =>{
		form.value.validate().then(res => {

			if(res.valid){
				let data  = {}
				if(type.value === 'create'){
					data = {
						descript: info.descript,
						next_day: info.next_day
					}
					postdaily(data).then(() =>{
						uni.showToast({
							icon:"none",
							title : "添加成功"
						})
						setTimeout(()=>{
							uni.navigateBack()
						}, 500)
					})
				}else{
					data = {
						params :{
							descript: info.descript,
							next_day: info.next_day,
							id : id.value,
							sys_user_id : ""
						}
					}
					putdaily(data).then(() =>{
						uni.showToast({
							icon:"none",
							title : "编辑成功"
						})
						setTimeout(()=>{
							uni.navigateBack()
						}, 500)
					})
				}
			}
		})
	}
</script>

<style>

</style>